//
//  AttentionManager.m
//  YoucApp
//
//  Created by Liam on 11/27/12.
//
//

#import "AttentionManager.h"
#import "UserManager.h"
#import "YoucApi.h"
#import "YAReqUserAttention.h"
#import "iOSToast.h"
#import "Util.h"

@implementation AttentionManager

OBJC_IMPL_SINGLETON(AttentionManager);

- (YAReqUserAttention *)getReqUserAttentionWithGameId:(NSString *)gameId;
{
	YAReqUserAttention *reqUserAttention = [[[YAReqUserAttention alloc] init] autorelease];
	[[UserManager instance] fillObjectWithSession:reqUserAttention];
	
	reqUserAttention.gameId = gameId;
	
	return reqUserAttention;
}

- (void)addAttention:(NSString *)gameId
{
	if ([[UserManager instance] checkUserLogin])
	{
		YAReqUserAttention *reqUserAttention = [self getReqUserAttentionWithGameId:gameId];
		[[YoucApi instance] userAddAttention:reqUserAttention context:self delegate:self];
	}
}

- (void)deleteAttention:(NSString *)gameId
{
	if ([[UserManager instance] checkUserLogin])
	{
		YAReqUserAttention *reqUserAttention = [self getReqUserAttentionWithGameId:gameId];
		[[YoucApi instance] userDeleteAttention:reqUserAttention context:self delegate:self];
	}
}

- (void)processAddAttentionResponse:(YARespUserAttention *)respUserAttention
{
	if (![NSThread isMainThread])
	{
		[self performSelectorOnMainThread:@selector(processAddAttentionResponse:) withObject:respUserAttention waitUntilDone:NO];
		
		return;
	}
	
//	UIViewController *rootViewController = [Util rootViewController];
//	
//	if (rootViewController)
//	{
//		NSString *msg = @"添加关注成功";
//		if ([respUserAttention.error code] != YoucApiErrorSuccess)
//		{
//			msg = [NSString stringWithFormat:@"添加关注: %@", [respUserAttention.error localizedDescription]];
//		}
//		
//		[rootViewController.view makeToast:msg duration:1.5f position:kDefaultPosition];
//	}
	
	[[NSNotificationCenter defaultCenter] postNotificationName:AMAddAttentionNotification object:respUserAttention userInfo:nil];
}

- (void)processDeleteAttentionResponse:(YARespUserAttention *)respUserAttention
{
	if (![NSThread isMainThread])
	{
		[self performSelectorOnMainThread:@selector(processDeleteAttentionResponse:) withObject:respUserAttention waitUntilDone:NO];
		
		return;
	}

	[[NSNotificationCenter defaultCenter] postNotificationName:AMDeleteAttentionNotification object:respUserAttention userInfo:nil];
}

- (void)userAddAttentionResponse:(id)task respUserAttention:(YARespUserAttention *)respUserAttention
{
	[self processAddAttentionResponse:respUserAttention];
}

- (void)userDeleteAttentionResponse:(id)task respUserAttention:(YARespUserAttention *)respUserAttention
{
	[self processDeleteAttentionResponse:respUserAttention];
}

@end
